﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LiJu.Product
{
    /// <summary>
    /// 自定义螺丝控件
    /// </summary>
    public partial class ScrewControl : Control
    {
        /// <summary>
        /// 套筒图片
        /// </summary>
        public PictureBox pic1 = null;
        /// <summary>
        /// 套筒编号
        /// </summary>
        public Label picNum = new Label();
        private Timer timer;

        private bool selected = false;
        /// <summary>
        /// 选中状态
        /// </summary>
        public bool Selected
        {
            get { return selected; }
            set
            {
                selected = value;
                if (selected)
                {
                    this.BackGroundColor = ScrewColor.selectedColor;
                    //Lbl_Position.Visible = true;
                    //Lbl_Position.Text = ScrewLocation;
                }
                else
                {
                    this.BackGroundColor = ScrewColor.addColor;
                    //Lbl_Position.Visible = false;
                }
                SelectEvent?.Invoke(this,null);
            }
        }

        private Color circleColor = ScrewColor.addColor;
        /// <summary>
        /// 控件颜色
        /// </summary>
        public Color BackGroundColor
        {
            get { return circleColor; }
            set
            {
                circleColor = value;
                this.Invalidate(); // 重绘控件
            }
        }

        private TestStatus status = TestStatus.Unknow;
        /// <summary>
        /// 状态
        /// </summary>
        public TestStatus Status
        {
            get { return status; }
            set
            {
                status = value;
            }
        }

        private Point controlPositon;
        /// <summary>
        /// 螺丝控件坐标
        /// </summary>
        public Point ControlPositon
        {
            get { return controlPositon; }
            set
            {
                controlPositon = value;
            }
        }

        private string screwLocation;
        /// <summary>
        /// 电批位置
        /// </summary>
        public string ScrewLocation
        {
            get { return screwLocation; }
            set
            {
                if(value!=null)
                {
                    screwLocation = value;
                    //string[] locations = value.Split(',');
                    //if (locations.Length > 1)
                    //{
                    //    screwLocation = $"x:{locations[0]}\r\ny:{locations[1]}";
                    //}
                    //else
                    //{
                    //    screwLocation = value;
                    //}
                }
            }
        }

        private double screwX = 0;
        /// <summary>
        /// 电批X轴坐标
        /// </summary>
        public double ScrewX
        {
            get { return screwX; }
            set
            {
                screwX = value;
            }
        }

        private double screwY = 0;
        /// <summary>
        /// 电批Y轴坐标
        /// </summary>
        public double ScrewY
        {
            get { return screwY; }
            set
            {
                screwY = value;
            }
        }
        private int worknum = 0;
        /// <summary>
        /// 工序
        /// </summary>
        public int WorkNum
        {
            get { return worknum; }
            set
            {
                worknum = value;
            }
        }


        private int num = 0;
        /// <summary>
        /// 编号
        /// </summary>
        public int Num
        {
            get { return num; }
            set
            {
                num = value;
                this.Text = num.ToString();
                this.Invalidate(); // 重绘控件
            }
        }

        private int imgNum = 0;
        /// <summary>
        /// 产品图片编号
        /// </summary>
        public int ImgNum
        {
            get { return imgNum; }
            set
            {
                imgNum = value;
            }
        }
        private int tightenPrepare;
        /// <summary>
        /// 是否预拧螺丝
        /// </summary>
        public int TightenPrepare
        {
            get { return tightenPrepare; }
            set { tightenPrepare = value; }
        }
        /// <summary>
        /// 对预拧螺丝进行半圆0,全圆1 显示控制
        /// </summary>
        private int circleState = 1;
        /// <summary>
        /// 对预拧螺丝进行半圆0,全圆1 显示控制
        /// </summary>
        public int CircleState
        {
            get { return circleState; }
            set { circleState = value; }
        }

        private object imgPath;
        /// <summary>
        /// 螺丝对于图片
        /// </summary>
        public object ImgPath
        {
            get { return imgPath; }
            set
            {
                imgPath = value;
            }
        }

        /// <summary>
        /// 螺丝位置标签(暂时隐藏)
        /// </summary>
        private Label Lbl_Position;

        //定义委托
        public delegate void SelectEventHandler(object sender, EventArgs e);

        /// <summary>
        /// 螺丝选择事件
        /// </summary>
        public event SelectEventHandler SelectEvent;

        /// <summary>
        /// 右键菜单
        /// </summary>
        public Action<Point, int,int> ContextMenuEvent;
        public ScrewControl()
        {
            InitializeComponent();
            this.Width = 50;
            this.Height = 50;
            num = 1;

            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.BackColor = Color.Transparent;

            Lbl_Position = new Label();
            Lbl_Position.Text = ScrewLocation;
            Lbl_Position.ForeColor = Color.Black;
            Lbl_Position.Visible = false;
            Lbl_Position.BringToFront();
            this.Controls.Add(Lbl_Position);

            pic1 = new PictureBox();
            pic1.Image = Properties.Resources.sleeve;// Image.FromFile("./Resources/sleeve.png");
            pic1.Dock = DockStyle.Fill;
            pic1.SizeMode = PictureBoxSizeMode.StretchImage;
            pic1.Visible = false;

            picNum.Text = "1";
            picNum.Font = new Font("黑体", 18, FontStyle.Bold);
            picNum.BackColor = Color.Transparent;
            picNum.AutoSize = true;        
            pic1.Controls.Add(picNum);
            this.Controls.Add(pic1);
            picNum.Location = new Point(this.Width / 2 - picNum.Width / 2, this.Height / 2 - picNum.Height / 2);

            timer = new Timer();
            timer.Interval = 500; // 设置动画帧率为100ms
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        /// <summary>
        /// 执行中螺丝定时旋转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_Tick(object sender, EventArgs e)
        {
            if (pic1.Visible)
            {
                pic1.BringToFront();
                pic1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                pic1.Refresh();
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Console.WriteLine($"1:{this.BackGroundColor}");
            base.OnPaint(e);
            Console.WriteLine($"2:{this.BackGroundColor}");
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            Console.WriteLine($"3:{this.BackGroundColor}");
            // 绘制圆形背景
            g.FillEllipse(new SolidBrush(this.BackGroundColor), 0, 0, this.Width, this.Height);
            Console.WriteLine($"4:{this.BackGroundColor}");
            //如果是预拧螺钉且状态非最终拧紧等条件成立下显示半圆
            if (this.FindForm().Text == "工作台" && this.tightenPrepare == 1 && this.worknum == this.num && this.circleState == 0)
            {
                // 绘制一个半圆路径
                GraphicsPath path = new GraphicsPath();
                path.AddArc(new Rectangle(1, 1, this.Width - 2, this.Height - 2), 180, 180);// 绘制半圆
                path.CloseAllFigures(); // 关闭路径
                // 使用路径创建一个区域
                Region region = new Region(path);
                // 填充半圆区域
                e.Graphics.FillRegion(new SolidBrush(Color.White), region);
            }
            

            // 绘制文本
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;
            Font font = new Font("黑体", 18, FontStyle.Bold);
            g.DrawString(this.Text, font, new SolidBrush(this.ForeColor), new RectangleF(0, 0, this.Width, this.Height), format);
            Console.WriteLine($"5:{this.BackGroundColor}");
        }
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            this.Height = this.Width; // 保持宽度和高度相同以形成圆形
        }

        /// <summary>
        /// 重写OnMouseEnter
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.Cursor = Cursors.Hand;
        }

        /// <summary>
        /// MouseUp事件，修改选中状态，修改选中样式
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            Selected = !Selected;
            if (e.Button == MouseButtons.Right)
            {
                Point mousePos = Control.MousePosition;
                ContextMenuEvent?.Invoke(mousePos,this.WorkNum, this.Num);
            }
        }

    }
}
